package org.example.Stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/** @ClassName: StreamLikePage @Author NeySun @Description: TODO @Version 1.0 @Date 2022-03-04 */
public class StreamLikePage {

  public static void main(String[] args) {

    List<Userz> userzList = new ArrayList<>();
    userzList.add(new Userz("chen", 10));
    userzList.add(new Userz("liu", 9));
    userzList.add(new Userz("han1", 20));
    userzList.add(new Userz("han2", 20));
    userzList.add(new Userz("han3", 20));
    userzList.add(new Userz("han4", 20));
    userzList.add(new Userz("han45", 20));
    userzList.add(new Userz("han11", 20));
    userzList.add(new Userz("han25", 20));
    String searchName = "h";
    // 是否全部匹配,还是 局部包含匹配
    Boolean globalMatch = Boolean.FALSE;
    int pageNum = 1; // 获取当前页
    int pageCount = 9; // 一页返回多少条

    List<Userz> rUseres =
        userzList.stream()
            // 模糊查询和等于查询
            .filter(
                userz ->
                    globalMatch
                        ? userz.getName().equals(searchName)
                        : userz.getName().contains(searchName))
            .sorted(Comparator.comparing(Userz::getAge))
            . // 按年纪升序 排序
            collect(Collectors.toList());
    // 一页 返回一条
    List<Userz> pageList =
        rUseres.stream()
            .skip((pageNum - 1) * pageCount)
            .limit(pageCount)
            .collect(Collectors.toList());
    System.out.println("返回总条数:" + rUseres.size() + "当前页为:" + pageNum + "每页显示条数:" + pageCount);
    System.out.println("本次返回的数据为:");
    pageList.forEach(System.out::println);
  }
}

class Userz {

  private String name;
  private Integer age;

  public Userz(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "Userz{" + "name='" + name + '\'' + ", age=" + age + '}';
  }
}
